home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / js / jsatom.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-05-08  |  17.6 KB  |  469 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Mozilla Communicator client code, released
  17.  * March 31, 1998.
  18.  *
  19.  * The Initial Developer of the Original Code is
  20.  * Netscape Communications Corporation.
  21.  * Portions created by the Initial Developer are Copyright (C) 1998
  22.  * the Initial Developer. All Rights Reserved.
  23.  *
  24.  * Contributor(s):
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  28.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. #ifndef jsatom_h___
  41. #define jsatom_h___
  42. /*
  43.  * JS atom table.
  44.  */
  45. #include <stddef.h>
  46. #include "jstypes.h"
  47. #include "jshash.h" /* Added by JSIFY */
  48. #include "jsapi.h"
  49. #include "jsprvtd.h"
  50. #include "jspubtd.h"
  51.  
  52. #ifdef JS_THREADSAFE
  53. #include "jslock.h"
  54. #endif
  55.  
  56. JS_BEGIN_EXTERN_C
  57.  
  58. #define ATOM_PINNED     0x01            /* atom is pinned against GC */
  59. #define ATOM_INTERNED   0x02            /* pinned variant for JS_Intern* API */
  60. #define ATOM_MARK       0x04            /* atom is reachable via GC */
  61. #define ATOM_HIDDEN     0x08            /* atom is in special hidden subspace */
  62. #define ATOM_NOCOPY     0x40            /* don't copy atom string bytes */
  63. #define ATOM_TMPSTR     0x80            /* internal, to avoid extra string */
  64.  
  65. struct JSAtom {
  66.     JSHashEntry         entry;          /* key is jsval, value keyword info or
  67.                                            unhidden atom if ATOM_HIDDEN */
  68.     uint32              flags;          /* pinned, interned, and mark flags */
  69.     jsatomid            number;         /* atom serial number and hash code */
  70. };
  71.  
  72. #define ATOM_KEY(atom)            ((jsval)(atom)->entry.key)
  73. #define ATOM_IS_OBJECT(atom)      JSVAL_IS_OBJECT(ATOM_KEY(atom))
  74. #define ATOM_TO_OBJECT(atom)      JSVAL_TO_OBJECT(ATOM_KEY(atom))
  75. #define ATOM_IS_INT(atom)         JSVAL_IS_INT(ATOM_KEY(atom))
  76. #define ATOM_TO_INT(atom)         JSVAL_TO_INT(ATOM_KEY(atom))
  77. #define ATOM_IS_DOUBLE(atom)      JSVAL_IS_DOUBLE(ATOM_KEY(atom))
  78. #define ATOM_TO_DOUBLE(atom)      JSVAL_TO_DOUBLE(ATOM_KEY(atom))
  79. #define ATOM_IS_STRING(atom)      JSVAL_IS_STRING(ATOM_KEY(atom))
  80. #define ATOM_TO_STRING(atom)      JSVAL_TO_STRING(ATOM_KEY(atom))
  81. #define ATOM_IS_BOOLEAN(atom)     JSVAL_IS_BOOLEAN(ATOM_KEY(atom))
  82. #define ATOM_TO_BOOLEAN(atom)     JSVAL_TO_BOOLEAN(ATOM_KEY(atom))
  83.  
  84. /*
  85.  * Return a printable, lossless char[] representation of a string-type atom.
  86.  * The lifetime of the result extends at least until the next GC activation,
  87.  * longer if cx's string newborn root is not overwritten.
  88.  */
  89. extern JS_FRIEND_API(const char *)
  90. js_AtomToPrintableString(JSContext *cx, JSAtom *atom);
  91.  
  92. #define ATOM_KEYWORD(atom)        ((struct keyword *)(atom)->entry.value)
  93. #define ATOM_SET_KEYWORD(atom,kw) ((atom)->entry.value = (kw))
  94.  
  95. struct JSAtomListElement {
  96.     JSHashEntry         entry;
  97. };
  98.  
  99. #define ALE_ATOM(ale)   ((JSAtom *) (ale)->entry.key)
  100. #define ALE_INDEX(ale)  ((jsatomid) JS_PTR_TO_UINT32((ale)->entry.value))
  101. #define ALE_JSOP(ale)   ((JSOp) (ale)->entry.value)
  102. #define ALE_VALUE(ale)  ((jsval) (ale)->entry.value)
  103. #define ALE_NEXT(ale)   ((JSAtomListElement *) (ale)->entry.next)
  104.  
  105. #define ALE_SET_ATOM(ale,atom)  ((ale)->entry.key = (const void *)(atom))
  106. #define ALE_SET_INDEX(ale,index)((ale)->entry.value = JS_UINT32_TO_PTR(index))
  107. #define ALE_SET_JSOP(ale,op)    ((ale)->entry.value = JS_UINT32_TO_PTR(op))
  108. #define ALE_SET_VALUE(ale,val)  ((ale)->entry.value = (JSHashEntry *)(val))
  109. #define ALE_SET_NEXT(ale,link)  ((ale)->entry.next = (JSHashEntry *)(link))
  110.  
  111. struct JSAtomList {
  112.     JSAtomListElement   *list;          /* literals indexed for mapping */
  113.     JSHashTable         *table;         /* hash table if list gets too long */
  114.     jsuint              count;          /* count of indexed literals */
  115. };
  116.  
  117. #define ATOM_LIST_INIT(al)  ((al)->list = NULL, (al)->table = NULL,           \
  118.                              (al)->count = 0)
  119.  
  120. #define ATOM_LIST_SEARCH(_ale,_al,_atom)                                      \
  121.     JS_BEGIN_MACRO                                                            \
  122.         JSHashEntry **_hep;                                                   \
  123.         ATOM_LIST_LOOKUP(_ale, _hep, _al, _atom);                             \
  124.     JS_END_MACRO
  125.  
  126. #define ATOM_LIST_LOOKUP(_ale,_hep,_al,_atom)                                 \
  127.     JS_BEGIN_MACRO                                                            \
  128.         if ((_al)->table) {                                                   \
  129.             _hep = JS_HashTableRawLookup((_al)->table, _atom->number, _atom); \
  130.             _ale = *_hep ? (JSAtomListElement *) *_hep : NULL;                \
  131.         } else {                                                              \
  132.             JSAtomListElement **_alep = &(_al)->list;                         \
  133.             _hep = NULL;                                                      \
  134.             while ((_ale = *_alep) != NULL) {                                 \
  135.                 if (ALE_ATOM(_ale) == (_atom)) {                              \
  136.                     /* Hit, move atom's element to the front of the list. */  \
  137.                     *_alep = ALE_NEXT(_ale);                                  \
  138.                     ALE_SET_NEXT(_ale, (_al)->list);                          \
  139.                     (_al)->list = _ale;                                       \
  140.                     break;                                                    \
  141.                 }                                                             \
  142.                 _alep = (JSAtomListElement **)&_ale->entry.next;              \
  143.             }                                                                 \
  144.         }                                                                     \
  145.     JS_END_MACRO
  146.  
  147. struct JSAtomMap {
  148.     JSAtom              **vector;       /* array of ptrs to indexed atoms */
  149.     jsatomid            length;         /* count of (to-be-)indexed atoms */
  150. };
  151.  
  152. struct JSAtomState {
  153.     JSRuntime           *runtime;       /* runtime that owns us */
  154.     JSHashTable         *table;         /* hash table containing all atoms */
  155.     jsatomid            number;         /* one beyond greatest atom number */
  156.     jsatomid            liveAtoms;      /* number of live atoms after last GC */
  157.  
  158.     /* The rt->emptyString atom, see jsstr.c's js_InitRuntimeStringState. */
  159.     JSAtom              *emptyAtom;
  160.  
  161.     /* Type names and value literals. */
  162.     JSAtom              *typeAtoms[JSTYPE_LIMIT];
  163.     JSAtom              *booleanAtoms[2];
  164.     JSAtom              *nullAtom;
  165.  
  166.     /* Various built-in or commonly-used atoms, pinned on first context. */
  167.     JSAtom              *ArgumentsAtom;
  168.     JSAtom              *ArrayAtom;
  169.     JSAtom              *BooleanAtom;
  170.     JSAtom              *CallAtom;
  171.     JSAtom              *DateAtom;
  172.     JSAtom              *ErrorAtom;
  173.     JSAtom              *FunctionAtom;
  174.     JSAtom              *MathAtom;
  175.     JSAtom              *NamespaceAtom;
  176.     JSAtom              *NumberAtom;
  177.     JSAtom              *ObjectAtom;
  178.     JSAtom              *QNameAtom;
  179.     JSAtom              *RegExpAtom;
  180.     JSAtom              *ScriptAtom;
  181.     JSAtom              *StringAtom;
  182.     JSAtom              *XMLAtom;
  183.     JSAtom              *FileAtom;
  184.     JSAtom              *anonymousAtom;
  185.     JSAtom              *argumentsAtom;
  186.     JSAtom              *arityAtom;
  187.     JSAtom              *calleeAtom;
  188.     JSAtom              *callerAtom;
  189.     JSAtom              *classPrototypeAtom;
  190.     JSAtom              *constructorAtom;
  191.     JSAtom              *countAtom;
  192.     JSAtom              *eachAtom;
  193.     JSAtom              *etagoAtom;
  194.     JSAtom              *evalAtom;
  195.     JSAtom              *getAtom;
  196.     JSAtom              *getterAtom;
  197.     JSAtom              *indexAtom;
  198.     JSAtom              *inputAtom;
  199.     JSAtom              *lengthAtom;
  200.     JSAtom              *nameAtom;
  201.     JSAtom              *namespaceAtom;
  202.     JSAtom              *noSuchMethodAtom;
  203.     JSAtom              *parentAtom;
  204.     JSAtom              *protoAtom;
  205.     JSAtom              *ptagcAtom;
  206.     JSAtom              *qualifierAtom;
  207.     JSAtom              *setAtom;
  208.     JSAtom              *setterAtom;
  209.     JSAtom              *spaceAtom;
  210.     JSAtom              *stagoAtom;
  211.     JSAtom              *starAtom;
  212.     JSAtom              *starQualifierAtom;
  213.     JSAtom              *tagcAtom;
  214.     JSAtom              *toLocaleStringAtom;
  215.     JSAtom              *toSourceAtom;
  216.     JSAtom              *toStringAtom;
  217.     JSAtom              *valueOfAtom;
  218.     JSAtom              *xmlAtom;
  219.  
  220.     /* Less frequently used atoms, pinned lazily by JS_ResolveStandardClass. */
  221.     struct {
  222.         JSAtom          *AnyNameAtom;
  223.         JSAtom          *AttributeNameAtom;
  224.         JSAtom          *EvalErrorAtom;
  225.         JSAtom          *InfinityAtom;
  226.         JSAtom          *InternalErrorAtom;
  227.         JSAtom          *NaNAtom;
  228.         JSAtom          *RangeErrorAtom;
  229.         JSAtom          *ReferenceErrorAtom;
  230.         JSAtom          *SyntaxErrorAtom;
  231.         JSAtom          *TypeErrorAtom;
  232.         JSAtom          *URIErrorAtom;
  233.         JSAtom          *XMLListAtom;
  234.         JSAtom          *decodeURIAtom;
  235.         JSAtom          *decodeURIComponentAtom;
  236.         JSAtom          *defineGetterAtom;
  237.         JSAtom          *defineSetterAtom;
  238.         JSAtom          *encodeURIAtom;
  239.         JSAtom          *encodeURIComponentAtom;
  240.         JSAtom          *escapeAtom;
  241.         JSAtom          *functionNamespaceURIAtom;
  242.         JSAtom          *hasOwnPropertyAtom;
  243.         JSAtom          *isFiniteAtom;
  244.         JSAtom          *isNaNAtom;
  245.         JSAtom          *isPrototypeOfAtom;
  246.         JSAtom          *isXMLNameAtom;
  247.         JSAtom          *lookupGetterAtom;
  248.         JSAtom          *lookupSetterAtom;
  249.         JSAtom          *parseFloatAtom;
  250.         JSAtom          *parseIntAtom;
  251.         JSAtom          *propertyIsEnumerableAtom;
  252.         JSAtom          *unescapeAtom;
  253.         JSAtom          *unevalAtom;
  254.         JSAtom          *unwatchAtom;
  255.         JSAtom          *watchAtom;
  256.     } lazy;
  257.  
  258. #ifdef JS_THREADSAFE
  259.     JSThinLock          lock;
  260.     volatile uint32     tablegen;
  261. #endif
  262. #ifdef NARCISSUS
  263.     JSAtom              *callAtom;
  264.     JSAtom              *constructAtom;
  265.     JSAtom              *hasInstanceAtom;
  266.     JSAtom              *ExecutionContextAtom;
  267.     JSAtom              *currentAtom;
  268. #endif
  269. };
  270.  
  271. /* Well-known predefined strings and their atoms. */
  272. extern const char   *js_type_str[];
  273. extern const char   *js_boolean_str[];
  274.  
  275. extern const char   js_Arguments_str[];
  276. extern const char   js_Array_str[];
  277. extern const char   js_Boolean_str[];
  278. extern const char   js_Call_str[];
  279. extern const char   js_Date_str[];
  280. extern const char   js_Function_str[];
  281. extern const char   js_Math_str[];
  282. extern const char   js_Namespace_str[];
  283. extern const char   js_Number_str[];
  284. extern const char   js_Object_str[];
  285. extern const char   js_QName_str[];
  286. extern const char   js_RegExp_str[];
  287. extern const char   js_Script_str[];
  288. extern const char   js_String_str[];
  289. extern const char   js_XML_str[];
  290. extern const char   js_File_str[];
  291. extern const char   js_anonymous_str[];
  292. extern const char   js_arguments_str[];
  293. extern const char   js_arity_str[];
  294. extern const char   js_callee_str[];
  295. extern const char   js_caller_str[];
  296. extern const char   js_class_prototype_str[];
  297. extern const char   js_constructor_str[];
  298. extern const char   js_count_str[];
  299. extern const char   js_etago_str[];
  300. extern const char   js_each_str[];
  301. extern const char   js_eval_str[];
  302. extern const char   js_getter_str[];
  303. extern const char   js_get_str[];
  304. extern const char   js_index_str[];
  305. extern const char   js_input_str[];
  306. extern const char   js_length_str[];
  307. extern const char   js_name_str[];
  308. extern const char   js_namespace_str[];
  309. extern const char   js_noSuchMethod_str[];
  310. extern const char   js_object_str[];
  311. extern const char   js_parent_str[];
  312. extern const char   js_private_str[];
  313. extern const char   js_proto_str[];
  314. extern const char   js_ptagc_str[];
  315. extern const char   js_qualifier_str[];
  316. extern const char   js_setter_str[];
  317. extern const char   js_set_str[];
  318. extern const char   js_space_str[];
  319. extern const char   js_stago_str[];
  320. extern const char   js_star_str[];
  321. extern const char   js_starQualifier_str[];
  322. extern const char   js_tagc_str[];
  323. extern const char   js_toSource_str[];
  324. extern const char   js_toString_str[];
  325. extern const char   js_toLocaleString_str[];
  326. extern const char   js_valueOf_str[];
  327. extern const char   js_xml_str[];
  328.  
  329. #ifdef NARCISSUS
  330. extern const char   js_call_str[];
  331. extern const char   js_construct_str[];
  332. extern const char   js_hasInstance_str[];
  333. extern const char   js_ExecutionContext_str[];
  334. extern const char   js_current_str[];
  335. #endif
  336.  
  337. /*
  338.  * Initialize atom state.  Return true on success, false with an out of
  339.  * memory error report on failure.
  340.  */
  341. extern JSBool
  342. js_InitAtomState(JSContext *cx, JSAtomState *state);
  343.  
  344. /*
  345.  * Free and clear atom state (except for any interned string atoms).
  346.  */
  347. extern void
  348. js_FreeAtomState(JSContext *cx, JSAtomState *state);
  349.  
  350. /*
  351.  * Interned strings are atoms that live until state's runtime is destroyed.
  352.  * This function frees all interned string atoms, and then frees and clears
  353.  * state's members (just as js_FreeAtomState does), unless there aren't any
  354.  * interned strings in state -- in which case state must be "free" already.
  355.  *
  356.  * NB: js_FreeAtomState is called for each "last" context being destroyed in
  357.  * a runtime, where there may yet be another context created in the runtime;
  358.  * whereas js_FinishAtomState is called from JS_DestroyRuntime, when we know
  359.  * that no more contexts will be created.  Thus we minimize garbage during
  360.  * context-free episodes on a runtime, while preserving atoms created by the
  361.  * JS_Intern*String APIs for the life of the runtime.
  362.  */
  363. extern void
  364. js_FinishAtomState(JSAtomState *state);
  365.  
  366. /*
  367.  * Atom garbage collection hooks.
  368.  */
  369. typedef void
  370. (*JSGCThingMarker)(void *thing, void *data);
  371.  
  372. extern void
  373. js_MarkAtomState(JSAtomState *state, uintN gcflags, JSGCThingMarker mark,
  374.                  void *data);
  375.  
  376. extern void
  377. js_SweepAtomState(JSAtomState *state);
  378.  
  379. extern JSBool
  380. js_InitPinnedAtoms(JSContext *cx, JSAtomState *state);
  381.  
  382. extern void
  383. js_UnpinPinnedAtoms(JSAtomState *state);
  384.  
  385. /*
  386.  * Find or create the atom for an object.  If we create a new atom, give it the
  387.  * type indicated in flags.  Return 0 on failure to allocate memory.
  388.  */
  389. extern JSAtom *
  390. js_AtomizeObject(JSContext *cx, JSObject *obj, uintN flags);
  391.  
  392. /*
  393.  * Find or create the atom for a Boolean value.  If we create a new atom, give
  394.  * it the type indicated in flags.  Return 0 on failure to allocate memory.
  395.  */
  396. extern JSAtom *
  397. js_AtomizeBoolean(JSContext *cx, JSBool b, uintN flags);
  398.  
  399. /*
  400.  * Find or create the atom for an integer value.  If we create a new atom, give
  401.  * it the type indicated in flags.  Return 0 on failure to allocate memory.
  402.  */
  403. extern JSAtom *
  404. js_AtomizeInt(JSContext *cx, jsint i, uintN flags);
  405.  
  406. /*
  407.  * Find or create the atom for a double value.  If we create a new atom, give
  408.  * it the type indicated in flags.  Return 0 on failure to allocate memory.
  409.  */
  410. extern JSAtom *
  411. js_AtomizeDouble(JSContext *cx, jsdouble d, uintN flags);
  412.  
  413. /*
  414.  * Find or create the atom for a string.  If we create a new atom, give it the
  415.  * type indicated in flags.  Return 0 on failure to allocate memory.
  416.  */
  417. extern JSAtom *
  418. js_AtomizeString(JSContext *cx, JSString *str, uintN flags);
  419.  
  420. extern JS_FRIEND_API(JSAtom *)
  421. js_Atomize(JSContext *cx, const char *bytes, size_t length, uintN flags);
  422.  
  423. extern JS_FRIEND_API(JSAtom *)
  424. js_AtomizeChars(JSContext *cx, const jschar *chars, size_t length, uintN flags);
  425.  
  426. /*
  427.  * This variant handles all value tag types.
  428.  */
  429. extern JSAtom *
  430. js_AtomizeValue(JSContext *cx, jsval value, uintN flags);
  431.  
  432. /*
  433.  * Convert v to an atomized string.
  434.  */
  435. extern JSAtom *
  436. js_ValueToStringAtom(JSContext *cx, jsval v);
  437.  
  438. /*
  439.  * Assign atom an index and insert it on al.
  440.  */
  441. extern JSAtomListElement *
  442. js_IndexAtom(JSContext *cx, JSAtom *atom, JSAtomList *al);
  443.  
  444. /*
  445.  * Get the atom with index i from map.
  446.  */
  447. extern JS_FRIEND_API(JSAtom *)
  448. js_GetAtom(JSContext *cx, JSAtomMap *map, jsatomid i);
  449.  
  450. /*
  451.  * For all unmapped atoms recorded in al, add a mapping from the atom's index
  452.  * to its address.  The GC must not run until all indexed atoms in atomLists
  453.  * have been mapped by scripts connected to live objects (Function and Script
  454.  * class objects have scripts as/in their private data -- the GC knows about
  455.  * these two classes).
  456.  */
  457. extern JS_FRIEND_API(JSBool)
  458. js_InitAtomMap(JSContext *cx, JSAtomMap *map, JSAtomList *al);
  459.  
  460. /*
  461.  * Free map->vector and clear map.
  462.  */
  463. extern JS_FRIEND_API(void)
  464. js_FreeAtomMap(JSContext *cx, JSAtomMap *map);
  465.  
  466. JS_END_EXTERN_C
  467.  
  468. #endif /* jsatom_h___ */
  469.